package tool.filterCase;

import org.testng.annotations.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bin.zhang
 * @version 2017年9月13日 下午3:02:05
 */
public class FilterEnterpriseCase {

    // static String otherDir = "D:\\Git\\project\\source\\V2.0\\src\\main\\java\\ccmsSystem\\testcase\\other";
    static String directory = "D:\\Git\\project\\source\\V2.0\\src\\main\\java\\ccmsSystem\\testcase\\stdMigrate";

    static List<String> enterpriseCaseIDs = new ArrayList<String>();

    {
        Scanner scan = null;
        try {
            scan = new Scanner(new FileInputStream(directory + File.separator + "enterpriseCase.txt"), "UTF-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (scan.hasNextLine()) {
            String enterpriseCaseID = scan.nextLine().split("-")[1].trim();
            enterpriseCaseIDs.add(enterpriseCaseID);
        }
        scan.close();
    }

    public static List<File> getAllCaseNotInEnterprise(File directoryFile) {
        List<File> filterFiles = new ArrayList<File>();
        File[] list = directoryFile.listFiles();
        if (list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                if (list[i].isFile() && list[i].getName().startsWith("TC_") && list[i].getName().endsWith(".java")) {

                    boolean isEnterCase = false;
                    for (int j = 0; j < enterpriseCaseIDs.size(); j++) {
                        if (list[i].getName().indexOf("_" + enterpriseCaseIDs.get(j) + "_") != -1) {
                            isEnterCase = true;
                            break;
                        }
                    }

                    if (!isEnterCase) {
                        filterFiles.add(list[i]);
                    }
                } else if (list[i].isDirectory()) {
                    List<File> filterFile = getAllCaseNotInEnterprise(list[i]);
                    if (filterFile.size() > 0) {
                        filterFiles.addAll(filterFile);
                    }
                }
            }
        }
        return filterFiles;
    }

    public static List<File> getAllCaseInEnterprise(File directoryFile) {
        List<File> filterFiles = new ArrayList<File>();
        File[] list = directoryFile.listFiles();
        if (list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                if (list[i].isFile() && list[i].getName().startsWith("TC_") && list[i].getName().endsWith(".java")) {
                    String id = null;
                    Pattern pattern = Pattern.compile("_(.*?)_");
                    Matcher matcher = pattern.matcher(list[i].getName());
                    if (matcher.find()) {
                        id = matcher.group(1);
                    }
                    if (id != null && enterpriseCaseIDs.contains(id)) {
                        filterFiles.add(list[i]);
                    }
                } else if (list[i].isDirectory()) {
                    List<File> filterFile = getAllCaseInEnterprise(list[i]);
                    if (filterFile.size() > 0) {
                        filterFiles.addAll(filterFile);
                    }
                }
            }
        }
        return filterFiles;
    }

    @Test
    public void findAllSameID() {
        System.out.println("enterpriseCaseIDs.size()=" + enterpriseCaseIDs.size());
        List<File> filterCaseFiles = getAllCaseInEnterprise(new File(directory));
        System.out.println("filterCaseFiles.size()=" + filterCaseFiles.size());
        filterCaseFiles.forEach(x -> System.out.println(x.getName().split("_")[1]));
    }

    // @Test
    public void commentStdCaseExceptSameID() {

        System.out.println("enterpriseCaseIDs.size()=" + enterpriseCaseIDs.size());

        List<File> filterCaseFiles = getAllCaseNotInEnterprise(new File(directory));
        System.out.println("filterCaseFiles.size()=" + filterCaseFiles.size());

        filterCaseFiles.forEach(x -> {
            System.out.println(x.getName());
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(x), "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                StringBuffer sb = new StringBuffer();
                String read = null;
                while ((read = br.readLine()) != null) {
                    sb.append("//" + read + "\n");
                }
                br.close();
                isr.close();

                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(x), "UTF-8");
                BufferedWriter bw = new BufferedWriter(osw);
                if (sb.length() != 0) {
                    bw.write(sb.toString());
                }
                bw.close();
                osw.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        });

        System.out.println("OK");
    }

}
